import Vue from 'vue';
// FIXME(CFY) 使用 script js 引入
// import VueI18n from 'vue-i18n';
import Cookies from 'js-cookie';
import enLocale from './en';
import zhLocale from './zh';

Vue.use(VueI18n);
const messages = {
  en: {
    ...enLocale,
  },
  zh: {
    ...zhLocale,
  },
};

export function getLanguage() {
  const chooseLanguage = Cookies.get('language');
  if (chooseLanguage) return chooseLanguage;

  // if has not choose language
  const language = (
    navigator.language || navigator.browserLanguage
  ).toLowerCase();
  const locales = Object.keys(messages);
  for (const locale of locales) {
    if (language.indexOf(locale) > -1) {
      Cookies.set('language', locale);
      return locale;
    }
  }
  Cookies.set('language', 'en');
  return 'en';
}

const i18n = new VueI18n({
  locale: getLanguage(), // 语言标识
  messages,
});

export default i18n;
